<?php

include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");

error_reporting(E_ERROR | E_PARSE);

//error_reporting(E_ALL);
//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	cgi::both($g_cgival["user_name"], "user_name", "");
	cgi::both($g_cgival["password"], "password", "");
	
	//去JS和HTML标签
	$g_cgival['user_name'] 	= string::un_script_code($g_cgival['user_name']);
	$g_cgival['user_name'] 	= string::un_html($g_cgival['user_name']);
	
	$g_cgival['password'] 	= string::un_script_code($g_cgival['password']);
	$g_cgival['password'] 	= string::un_html($g_cgival['password']);
	
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	$user_name = $g_cgival['user_name'];
	$password = $g_cgival['password'];
	$user_pic = "/images/user_icon/default/default_1.jpg";
	$user_info = new user_info();
	if( is_numeric($g_cgival['user_name']))
	{
		throw new Exception("不允许用纯数字帐号！");
		
	}
	
	if(strlen($g_cgival['user_name']) < 5)
	{
		throw new Exception("帐号太短了。至少需要5个字母");
	}
	
	$insert_uid= $user_info->insert_user_info($user_name,$password,$user_pic);

	if(!$insert_uid)
	{
		throw new Exception("注册失败");
	}
	else
	{
		$set_cookie_res= $user_info->set_cookie_by_user_info($insert_uid,$user_name);
		if(!$set_cookie_res)
		{
			throw new Exception("COOKIE ERROR");
		}
		
	}
	
}
try {
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = "";
	
	
} 
catch (Exception $e) 
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
	
}
show_pro();

exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	echo json_encode($g_show);

	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
}

